package com.ruoyi.kjy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.kjy.domain.Payment;
import com.ruoyi.kjy.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Objects;

/**
 * @Description:
 * @Author: Hi，Wang
 * @Date: 4/13,0013 17:00
 */


@RestController
@RequestMapping("/payment")
public class PaymentController extends BaseController {


    @Autowired
    private PaymentService paymentService;

    @GetMapping("/list")
    public AjaxResult list(Long current, Long size, Payment payment) {
        QueryWrapper<Payment> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(payment);
        if (Objects.isNull(current)) {
            current = 1l;
        }
        if (Objects.isNull(size)) {
            size = 10l;
        }
        if (size >= 20l) {
            size = 20l;
        }
        return AjaxResult.success(paymentService.page(new Page<>(current, size), queryWrapper));
    }


    @GetMapping("/listMy")
    public AjaxResult listMy(Long current, Long size, Payment payment) {
        QueryWrapper<Payment> queryWrapper = new QueryWrapper<>();
        payment.setUserId(SecurityUtils.getUserId());
        queryWrapper.setEntity(payment);
        if (Objects.isNull(current)) {
            current = 1l;
        }
        if (Objects.isNull(size)) {
            size = 10l;
        }
        if (size >= 20l) {
            size = 20l;
        }
        return AjaxResult.success(paymentService.page(new Page<>(current, size), queryWrapper));
    }


    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(paymentService.getById(id));
    }


    @Log(title = "缴费", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Payment payment) {
        payment.setUserId(SecurityUtils.getUserId());
        payment.setStatus(0);
        boolean save = paymentService.save(payment);
        return toAjax(save);
    }

    @Log(title = "缴费", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Payment payment) {
        return toAjax(paymentService.updateById(payment));
    }

    @Log(title = "缴费", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable(value = "ids") Long[] ids) {
        return toAjax(paymentService.removeByIds(Arrays.asList(ids)));
    }

}
